﻿using System;
using System.Collections.Generic;
using System.Text;
using BaBaoFan.Modifier.Data;
using BaBaoFan.Modifier.Model;
using System.Linq;

namespace BaBaoFan.Modifier.Service
{
    public class NavigationRelationService : INavigationRelationService
    {
        private readonly INavigationRelationRepository navigationRelationRepository;
        public NavigationRelationService(INavigationRelationRepository navigationRelationRepository)
        {
            this.navigationRelationRepository = navigationRelationRepository;
        }
        public bool Add(NavigationRelationEntity entity)
        {
            return navigationRelationRepository.Add(entity);
        }

        public bool AddNavigationInfoAndRelations(NavigationInfoEntity navigation, IEnumerable<NavigationRelationEntity> relationEntities)
        {
            return navigationRelationRepository.AddNavigationInfoAndRelations(navigation, relationEntities);
        }
        public bool UpdateNavigationInfoAndRelations(NavigationInfoEntity navigation, IEnumerable<NavigationRelationEntity> relationEntities)
        {
            var oldRelations = navigationRelationRepository.GetByNavigationId(navigation.Id).ToList();
            var newList = new List<NavigationRelationEntity>();//要新建的关系
            var delList = new List<NavigationRelationEntity>();//要删除的关系

            foreach (var item in relationEntities)
            {
                if (!oldRelations.Exists(r => r.ClassId == item.ClassId))
                {
                    newList.Add(item);
                }
            }
            foreach (var item in oldRelations)
            {
                if (!relationEntities.ToList().Exists(r => r.ClassId == item.ClassId))
                {
                    delList.Add(item);
                }
            }
            return navigationRelationRepository.UpdateNavigationInfoAndRelations(navigation, delList, newList);
        }
        public bool Delete(int[] ids)
        {
            return navigationRelationRepository.Delete(ids);
        }

        public IEnumerable<NavigationRelation> GetByClassId(int id)
        {
            return navigationRelationRepository.GetByClassId(id);
        }

        public NavigationRelationEntity GetById(int id)
        {
            return navigationRelationRepository.GetById(id);
        }

        public IEnumerable<NavigationRelation> GetNavigationRelations()
        {
            return navigationRelationRepository.GetNavigationRelations();
        }

        public bool Update(NavigationRelationEntity entity)
        {
            return navigationRelationRepository.Update(entity);
        }

        public IEnumerable<NavigationRelationEntity> GetByNavigationId(int id)
        {
            return navigationRelationRepository.GetByNavigationId(id);
        }

        public IEnumerable<NavigationRelation> Search(RelationCondition condition)
        {
            return navigationRelationRepository.Search(condition);
        }
    }
}
